package pta07;

import java.util.Scanner;

//http://blog.csdn.net/woainiwss/article/details/52013810
class Repo{
	private int i=0;
	private String[] str;
	private boolean flag=false;
	public Repo(String items){
		str=items.split(" ");
	}
	public synchronized int getSize(){
		return str.length-i;
	}
	public synchronized void jiaoti1(){
		while(getSize()>0){
			if(flag){
				try{
					wait();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
			else {
				System.out.println(Thread.currentThread().getName()+" finish "+str[i]);
				i++;
				flag=true;
				notify();
			}
		}
	}
	public synchronized void jiaoti2(){
		while(getSize()>0){
			if(!flag){
				try{
					wait();
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
			else {
				System.out.println(Thread.currentThread().getName()+" finish "+str[i]);
				i++;
				flag=false;
				notify();
			}
		}
	}
}
class Worker1 implements Runnable{
	private Repo repo;
    public Worker1(Repo repo){
    	this.repo=repo;
    }
	@Override
	public synchronized void run() {
		// TODO Auto-generated method stub
		repo.jiaoti1();
	}
}
class Worker2 implements Runnable{
	private Repo repo;
    public Worker2(Repo repo){
    	this.repo=repo;
    }
	@Override
	public synchronized void run() {
		// TODO Auto-generated method stub
		repo.jiaoti2();
	}
}
public class pta746_AlternateExecution {
	    public static void main(String[] args) throws InterruptedException {
	        Scanner sc = new Scanner(System.in);
	        Repo repo = new Repo(sc.nextLine());
	        Thread t1 = new Thread(new Worker1(repo));
	        Thread t2 = new Thread(new Worker2(repo));
	        t1.start();
	        Thread.yield();
	        t2.start();
	        sc.close();
	    }
}

